package com.ruoyi.system.util;
import com.ruoyi.system.domain.StandardSpider;
import com.ruoyi.system.domain.dto.StartSpiderDto;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class SpiderResponseUtil {

    public List<Map<String, Object>> sendSpiderRequest(StartSpiderDto startSpiderDto) throws Exception {
        String flaskUrl = "http://localhost:5000/process_spider_data"; // Flask API 的 URL
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(flaskUrl);
        httpPost.setHeader("Content-Type", "application/json");

        // 构造发送的数据
        JSONObject jsonData = new JSONObject();
        jsonData.put("standardCodes", startSpiderDto.getStandardCodes()); // String[]
        jsonData.put("params", startSpiderDto.getParams());       // String[]
        // 设置 JSON 请求实体
        StringEntity entity = new StringEntity(jsonData.toJSONString(), "UTF-8");
        httpPost.setEntity(entity);

        // 发送 POST 请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
        // 解析 Flask 返回的 JSON 响应
        String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");
        List<Map<String, Object>> spiderResponse = JSONObject.parseObject(responseString, List.class);
        response.close();
        httpClient.close();
        return spiderResponse;
    }

    // 将解析后的响应转换为StandardSpider对象
    public List<StandardSpider> convertToStandardSpiderList(List<Map<String, Object>> spiderResponse) {
        List<StandardSpider> spiderList = new ArrayList<>();

        for (Map<String, Object> spiderData : spiderResponse) {
            StandardSpider standardSpider = new StandardSpider();
            // 设置标准号
            standardSpider.setStandardCode((String) spiderData.get("standardCode"));
            // 设置参数值，将字符串转为Long类型
            standardSpider.seta11(parseLong(spiderData.get("a11")));
            standardSpider.seta12(parseLong(spiderData.get("a12")));
            standardSpider.seta13(parseLong(spiderData.get("a13")));
            standardSpider.setb11(parseLong(spiderData.get("b11")));
            standardSpider.setb12(parseLong(spiderData.get("b12")));
            standardSpider.setb13(parseLong(spiderData.get("b13")));
            standardSpider.setb14(parseLong(spiderData.get("b14")));
            standardSpider.setb21(parseLong(spiderData.get("b21")));
            standardSpider.setb22(parseLong(spiderData.get("b22")));
            standardSpider.setb23(parseLong(spiderData.get("b23")));
            standardSpider.setb24(parseLong(spiderData.get("b24")));
            standardSpider.setb31(parseLong(spiderData.get("b31")));
            standardSpider.setb32(parseLong(spiderData.get("b32")));
            standardSpider.setb33(parseLong(spiderData.get("b33")));
            standardSpider.setb41(parseLong(spiderData.get("b41")));
            standardSpider.setb42(parseLong(spiderData.get("b42")));
            standardSpider.setb43(parseLong(spiderData.get("b43")));
            // 添加到结果列表
            spiderList.add(standardSpider);
        }
        return spiderList;
    }
    // 辅助方法，将对象转换为Long类型
    private Long parseLong(Object value) {
        if (value == null) {
            return null;
        }
        try {
            return Long.parseLong(value.toString());
        } catch (NumberFormatException e) {
            return null; // 如果转换失败，返回null或根据需要处理
        }
    }

}
